﻿using System.Data.Metadata.Edm;
using SelfMadeMillionaire.Core.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace SelfMadeMillionaire.DAL
{
	public class DataContext : DbContext, IUnitOfWork
	{
		public IDbSet<Publication> Publications { get; set; }
        public IDbSet<PublicationProduct> PublicationProducts { get; set; }
		public IDbSet<Newsletter> Newsletters { get; set; }
        public IDbSet<NewsletterProduct> NewsletterProducts { get; set; }
        public IDbSet<NewsletterRead> NewsletterRead { get; set; }
        public IDbSet<News> News { get; set; }
        public IDbSet<NewsProduct> NewsProducts { get; set; }
		public IDbSet<Subscriber> Subscribers { get; set; }
		public IDbSet<EmailMessage> EmailMessages { get; set; }
		public IDbSet<TrackingPortfolioInfo> TrackingPortfolio { get; set; }
		public IDbSet<IssueType> IssueTypes { get; set; }
		public IDbSet<Scheduler> Schedulers { get; set; }
        public IDbSet<EmailMessageSend> EmailMessageSends { get; set; }

		public IDbSet<PayPalPayment> PayPalPayments { get; set; }
		public IDbSet<PayPalBuyer> PayPalBuyers { get; set; }
		public IDbSet<PayPalTransaction> PayPalTransactions { get; set; }
		public IDbSet<Order> Orders { get; set; }
        public IDbSet<Promotion> Promotion { get; set; }
        public IDbSet<Log> Log { get; set; }
        public IDbSet<LogVisit> LogVisit { get; set; }
        public IDbSet<EmailSend> EmailSend { get; set; }

        public IDbSet<Portfolio> Portfolio { get; set; }
        public IDbSet<Symbol> Symbol { get; set; }
        public IDbSet<SymbolOptionsHistory> SymbolOptionsHistory { get; set; }
        public IDbSet<UserPortfolio> UserPortfolio { get; set; }
        public IDbSet<UserSymbol> UserSymbol { get; set; }

        public IDbSet<CountryCode> CountryCode { get; set; }
        public IDbSet<UserSession> UserSession { get; set; }

        //public IDbSet<MailingOpenLog> MailingOpenLog { get; set; }

		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
	
			modelBuilder.Entity<Subscriber>()
							.HasMany(x => x.EmailMessages)
							.WithMany(x => x.Subscribers)
							.Map(x =>
									{
										x.ToTable("SubscriberMessage");
										x.MapLeftKey("SubscriberId");
										x.MapRightKey("EmailMessageId");
									});
		}

		public void Commit()
		{
			this.SaveChanges();
		}

		public IDbSet<T> GetDbSet<T>() where T : BaseEntity
		{
			return this.Set<T>();
		}
	}
}
